﻿using System;
using System.Collections.Generic;
using System.Linq;
using Crosland.Automotriz.Logic.Linq;
using DevExpress.Web.ASPxEditors;
using System.Web.UI;

namespace Crosland.Automotriz.Web
{
    public partial class wFrmClienteVendedor : Page
    {
        readonly clsTD_VendedorBL bl = new clsTD_VendedorBL();
        readonly clsTH_VentaBL blVenta = new clsTH_VentaBL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false) 
            {
                ListarSector();
            }
            if (cboSector.SelectedItem.Value.ToString() != "" && txtIdVendedor.Text != "")
            {
                CargarGrillaClientes();
            }
        }

        private void ListarSector() 
        {
            clsTD_SectorBL sectorBL = new clsTD_SectorBL();

            cboSector.DataSource = sectorBL.Listar();
            cboSector.ValueField = "ID_SECTOR";
            cboSector.TextField = "DES_SECTOR";
            cboSector.DataBind();

            ListEditItem l = new ListEditItem() { Text = "Seleccionar", Value = "" };
            cboSector.Items.Insert(0, l);
            cboSector.SelectedIndex = 0;
        }

        private void CargarGrillaVendedores() 
        {
            dgVendedor.FocusedRowIndex = -1;
            dgVendedor.DataSource = bl.Listar(txtBuscar.Text, Convert.ToInt32(cboOpcion.Text));
            dgVendedor.DataBind();
            txtBuscar.Focus();
        }

        private void CargarGrillaClientes() 
        {
            int Id_Sector = int.Parse(cboSector.SelectedItem.Value.ToString());
            int Id_Vendedor = int.Parse(txtIdVendedor.Text);
            int TIPO_RESULTADO = int.Parse(cboTipoResultado.Value.ToString());

            grdClientes.DataSource = blVenta.Listar(Id_Sector, Id_Vendedor, TIPO_RESULTADO);
            grdClientes.DataBind();
        }

        protected void btnLocales_Click(object sender, EventArgs e)
        {
            int indice = grdClientes.FocusedRowIndex;
            if (indice >= 0)
            {

                List<object> keys = grdClientes.GetSelectedFieldValues(new string[] { grdClientes.KeyFieldName });
                string idCliente = keys[0].ToString();

                Response.Redirect("wfrmLocalesCliente.aspx?idCliente=" + idCliente);
            }
        }

        protected void btnSalir_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfrmGestionComercial.aspx");
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            CargarGrillaVendedores();
        }

        protected void dgVendedor_PageIndexChanged(object sender, EventArgs e)
        {
            CargarGrillaVendedores();
        }

        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            if (cboSector.SelectedItem.Value.ToString() == "")
            {
                const string script = "ShowCreateAccountWindow7();";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "x", script, true);
                return;
            }
            if (txtIdVendedor.Text == "")
            {
                const string script = "ShowCreateAccountWindow7();";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "x", script, true);
                return;
            }
            CargarGrillaClientes();
        }
     }
}